﻿using System;
using Avalonia;
using Avalonia.Controls;
using JMClient.Assets.ClientDesignConfig;
using JMClient.Service;
using Microsoft.Extensions.Logging;
using Prism.Ioc;
using Prism.Mvvm;

namespace JMClient.ViewModels;

public partial class MainWindowViewModel : BindableBase
{
    private readonly ConfigService _configService;
    private readonly IContainerProvider _container;
    public string Greeting { get; } = "Welcome to Avalonia!";
    public MainWindowViewModel(ConfigService configService, IContainerProvider container)
    {
        _configService = configService;
        _container = container;
        ApplyThemeAndLayout();
    }

    public void ApplyThemeAndLayout()
    {
        if (Application.Current != null)
        {
            _configService.ApplyThemeSettings(Application.Current);
        }
    }

    public void ApplyWindowSettings(Window window)
    {
        _configService.ApplyWindowSettings(window);
    }

    public void SaveOnExit(Window window)
    {
        try
        {
            // 使用新的 AvaloniaWindowConfig 保存窗口状态
            _configService.SaveCurrentWindowState(window);
            
            // 保存配置到文件
            _configService.SaveConfiguration();
        }
        catch (Exception ex)
        {
            // 记录错误但不阻止窗口关闭
            System.Diagnostics.Debug.WriteLine($"保存窗口状态失败: {ex.Message}");
        }
    }
}